<template>
	<view>
		<view class="goods_list">
			<view v-for="(goods,index) in gooodsList" :key="index" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObject: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				gooodsList: [],
				total: 0,
				//定义一个节流阀，为false表明没有在请求数据
				isLoading: false
			};
		},

		onLoad(options) {
			// console.log(options);
			this.queryObject.query = options.query || ''
			this.queryObject.cid = options.cid || ''
			// console.log(this.queryObject);
			this.getGoodsList()
		},

		methods: {
			async getGoodsList(cb) {
				//打开节流阀，表明正在请求数据
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('api/public/v1/goods/search', this.queryObject)

				//关闭节流阀
				this.isLoading = false

				cb && cb()


				if (res.meta.status !== 200) return uni.$showMsg()
				// this.gooodsList = res.message.goods
				//新旧数据拼接
				this.gooodsList = [...this.gooodsList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}

		},

		//触底事件
		onReachBottom() {

			//判断下一页是否还有数据
			if (this.queryObject.pagenum * this.queryObject.pagesize >= this.total) return uni.$showMsg("数据加载完毕！！")


			//判断是否正在请求其他数据，如果是，则不发出额外的请求
			if (this.isLoading) return


			//让页码增加1
			this.queryObject.pagenum++
			this.getGoodsList()
		},

		onPullDownRefresh() {
			//重置关键数据
			this.queryObject.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.gooodsList = []

			//重新发起请求,并关闭下拉刷新效果
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}

	}
</script>

<style lang="scss">

</style>